package fts.ftsdata.services.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import fts.ftsdata.domain.IWatchFolder;
import fts.ftsdata.domain.IWatchWildcard;
import fts.ftsdata.domain.impl.WatchFolder;
import fts.ftsdata.domain.impl.WatchWildcard;
import fts.ftsdata.services.IConfigurationService;


public class ConfigurationService implements IConfigurationService
{
	SessionFactory factory;


	public ConfigurationService(SessionFactory fact)
	{
		factory = fact;
	}
	
	public void deleteWatchFolder(IWatchFolder watchFolder) throws Exception
	{
		System.out.println(Thread.currentThread().getStackTrace()[1]);
		Session sess = factory.openSession();
		try
		{
			sess.delete(watchFolder);
			sess.flush();
		}
		catch(Exception ex)
		{
			System.out.println(ex);
		}
		finally
		{
			sess.close();
		}	}

	@SuppressWarnings("unchecked")
	public Collection<WatchFolder> getAllWatchFolders() throws Exception
	{
		Session sess = factory.openSession();
		ArrayList<WatchFolder> resColl = new ArrayList<WatchFolder>();
		try
		{
			Criteria crit = sess.createCriteria(WatchFolder.class);
			List res = crit.list();
			Iterator iter = res.iterator();
			while(iter.hasNext())
			{
				resColl.add((WatchFolder) iter.next());
			}
			sess.flush();
		}
		catch(Exception ex)
		{
			System.out.println(ex);
			throw ex;
		}
		finally
		{
			sess.close();
		}
		return resColl;	
	}

	public void saveWatchFolder(IWatchFolder watchFolder) throws Exception
	{
		System.out.println(Thread.currentThread().getStackTrace()[1]);
		Session sess = factory.openSession();
		try
		{
			sess.saveOrUpdate(watchFolder);
			sess.flush();
		}
		catch(Exception ex)
		{
			System.out.println(ex);
		}
		finally
		{
			sess.close();
		}
	}

	public IWatchFolder getWatchFolder(String path) throws Exception
	{
		System.out.println(Thread.currentThread().getStackTrace()[1]);
		Session sess = factory.openSession();
		WatchFolder res = null;
		try
		{
			Criteria crit = sess.createCriteria(WatchFolder.class).add(Restrictions.ilike("path", path.toLowerCase().replaceAll("\\\\", "\\\\\\\\"), MatchMode.EXACT));
			res = (WatchFolder) crit.uniqueResult();
			return res;
		}
		catch(Exception ex)
		{
			System.out.println(ex);
			throw ex;
		}
		finally
		{
			sess.close();
		}
	}

	public IWatchFolder createWatchFolder(String path) throws Exception
	{
		IWatchFolder folder = getWatchFolder(path);
		if(folder == null)
		{
			folder = new WatchFolder();
			folder.setPath(path);
		}
		return folder;
	}

	public IWatchWildcard createWatchWildcard(String wildcard, IWatchFolder folder)
	{
		WatchWildcard wc = new WatchWildcard();
		wc.setWildcard(wildcard);
		wc.setWatchFolder((WatchFolder)folder);
		return wc;
	}


}
